﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class QQSdk : MonoBehaviour
{
    // 单例
    public static QQSdk Instance { get; private set; }

    private AndroidJavaClass jc = null;
    private AndroidJavaObject jo = null;

    private void Awake()
    {
        Instance = this;

        jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
        jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
    }

    /// <summary>
    /// 登录
    /// </summary>
    public void Login()
    {
        jo?.Call("login");
    }

    /// <summary>
    /// 注销
    /// </summary>
    public void Logout()
    {
        jo?.Call("logout");
    }

    /// <summary>
    /// 获取用户信息
    /// </summary>
    public void GetUserInfo()
    {
        jo?.Call("getUserInfo");
    }

    /// <summary>
    /// 获取UnionId
    /// </summary>
    public void GetUnionId()
    {
        jo.Call("getUnionId");
    }

    /// <summary>
    /// 分享到QQ消息
    /// </summary>
    /// <param name="title">标题</param>
    /// <param name="summary">概要内容</param>
    /// <param name="targetUrl">跳转URL</param>
    /// <param name="imgUrl">图片URL</param>
    /// <param name="appName">应用名称</param>
    public void ShareToQQ(string title, string summary, string targetUrl, string imgUrl = "", string appName = "")
    {
        jo?.Call("shareToQQ", title, summary, targetUrl, imgUrl, appName);
    }

    /// <summary>
    /// 分享到QQ空间
    /// </summary>
    /// <param name="title">标题</param>
    /// <param name="summary">概要内容</param>
    /// <param name="targetUrl">跳转URL</param>
    /// <param name="imgUrl">图片URL</param>
    /// <param name="appName">应用名称</param>
    public void ShareToQZone(string title, string summary, string targetUrl, string imgUrl = "", string appName = "")
    {
        jo?.Call("shareToQZone", title, summary, targetUrl, imgUrl, appName);
    }


    #region 需要Android调用的方法
    // 登录成功
    public void OnLoginComplete(string json)
    {
        GetUserInfo();  // 获取用户信息
        GetUnionId();   // 获取UnionId
    }

    // 获取用户信息成功
    public void OnGetUserInfoComplete(string json)
    {
        Test.Instance.ShowInfoPanel();
        Test.Instance.RefreshInfo(json);
    }
    
    // 获取UnionId成功
    public void OnGetUnionIdComplete(string json)
    {

    }

    // 注销登录
    public void OnLogout(string arg)
    {
        Test.Instance.ShowLoginPanel();
    }
    #endregion
}
